/**
 * Copyright (c) 2011-11-5 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.mail;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wkm.util.CharsetUtil;

/**
 * @author WKM
 * Modification history
 * 1.WKM - 2011-11-5 -Create File
 */
public class SendMail {

	private static Logger logger = LoggerFactory.getLogger(SendMail.class);
	
	public static void sendMail(MailBean mb)throws EmailException{
		logger.info("创建邮件：" + mb.getTitle());
		HtmlEmail htmlEmail = new HtmlEmail();
		htmlEmail.setAuthentication(mb.getUserName(), mb.getPassword());
		htmlEmail.setHostName(mb.getSmtp());
		htmlEmail.setFrom(mb.getFrom());
		htmlEmail.addTo(mb.getTo());
		htmlEmail.setSubject(mb.getTitle());
		htmlEmail.setCharset(CharsetUtil.UTF_8);
		htmlEmail.setHtmlMsg(mb.getMailContent());
		logger.info("准备发送邮件：" + mb.getTitle());
		htmlEmail.send();
		logger.info("发送完成！");
	}
	
	public static void main(String[] args) throws EmailException {
		MailBean mb = new MailBean();
		
		mb.setFrom("mewkmwkm@163.com");
		mb.setSmtp("smtp.163.com");
		mb.setTo("wkm.home@139.com");
		mb.setUserName("mewkmwkm");
		mb.setPassword("");
		mb.setTitle("邮件标题");
		mb.setMailContent("邮件正文！");
		SendMail.sendMail(mb);
	}
}
